package encaps.action;

import encaps.entities.Grant;
import encaps.entities.Role;
import encaps.logging.JMSUtil;
import encaps.logging.Log;
import encaps.security.RoleManager;
import encaps.utils.*;
import java.sql.SQLException;
import java.util.Date;
import javax.ejb.FinderException;
import javax.naming.NamingException;
import org.jdom.Element;

public class DeleteRoleAction extends Action {

    public static final String DELETE_ROLE = "deleteRole";
    protected ActionContext actionContext;

    public DeleteRoleAction(ActionContext context, Grant requiredGrant) {
        this.actionContext = context;
        this.requiredGrant = requiredGrant;
    }

    @Override
    public Element execute() throws ActionExecutionException {
        try {
            String urole = actionContext.getTextParameter(ID);
            RoleManager.getRoleManager().clearCache();
            JMSUtil.sendLogMessage(Log.DELETE,
                    EntityUtil.getUserHome().findByLogin(actionContext.getUserLogin()).getID(),
                    actionContext.getUserLogin(), new Date(), Role.TABLE_NAME,
                    null, urole);
            Provider.deleteValues(RoleGrantGenreEnum.ROLE, urole);
            return XMLHelper.createTextMessage("Role removed");
        } catch (NamingException ex) {
            throw new ActionExecutionException("Cannot find PesronLocalHome object", ex);
        } catch (FinderException ex) {
            throw new ActionExecutionException("Person not found", ex);
        } catch (DataAccessException ex) {
            throw new ActionExecutionException("System malfunction", ex);
        } catch (SystemException ex) {
            throw new ActionExecutionException("System malfunction", ex);
        } catch (SQLException ex) {
            throw new ActionExecutionException("Remove exception", ex);
        }

    }
}
